package com.mbyte.easy.task.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.task.entity.Task;
import com.mbyte.easy.task.service.ITaskService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/task/task")
public class TaskController extends BaseController  {

    private String prefix = "task/task/";

    @Autowired
    private ITaskService taskService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param task
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String creatTimeSpace, String deleteTimeSpace, Task task) {
        SysUser sysUser = Utility.getCurrentUser();
        System.out.println("------------");
        System.err.println(sysUser.getUsername());
        System.out.println();
        Page<Task> page = new Page<Task>(pageNo, pageSize);
        QueryWrapper<Task> queryWrapper = new QueryWrapper<Task>();
        if(!ObjectUtils.isEmpty(task.getTaskType())) {
                queryWrapper = queryWrapper.like("task_type",task.getTaskType());
         }
        if(!ObjectUtils.isEmpty(task.getTaskTitle())) {
            queryWrapper = queryWrapper.like("task_title",task.getTaskTitle());
         }
        if(!ObjectUtils.isEmpty(task.getTaskContent())) {
            queryWrapper = queryWrapper.like("task_content",task.getTaskContent());
         }
        if(!ObjectUtils.isEmpty(task.getTaskStatus())) {
            queryWrapper = queryWrapper.like("task_status",task.getTaskStatus());
         }
        if(!ObjectUtils.isEmpty(task.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",task.getUserId());
         }
        if(!ObjectUtils.isEmpty(task.getCreatTime())) {
            queryWrapper = queryWrapper.like("creat_time",task.getCreatTime());
         }
        if(!ObjectUtils.isEmpty(task.getDeleteTime())) {
            queryWrapper = queryWrapper.like("delete_time",task.getDeleteTime());
         }
        if(!ObjectUtils.isEmpty(task.getDeleteStatus())) {
            queryWrapper = queryWrapper.like("delete_status",task.getDeleteStatus());
         }
        IPage<Task> pageInfo = taskService.page(page, queryWrapper);
        model.addAttribute("creatTimeSpace", creatTimeSpace);
        model.addAttribute("deleteTimeSpace", deleteTimeSpace);
        model.addAttribute("searchInfo", task);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(Model model){
        SysUser currentUser = Utility.getCurrentUser();
        Long id = currentUser.getId();
        model.addAttribute("userId",id);
        return prefix+"add";
    }
    /**
    * 添加
    * @param task
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Task task){

        return toAjax(taskService.save(task));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("task",taskService.getById(id));
        return prefix+"edit";
    }
    /**
    * 添加
    * @param task
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Task task){
        return toAjax(taskService.updateById(task));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(taskService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(taskService.removeByIds(ids));
    }

}

